﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ProduceBasicData.Api.Application.Command.DeviceManage;
using ProduceBasicData.Domain.DeviceManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;
using ProduceBasicData.Api.Services; //编码

namespace ProduceBasicData.Api.Application.Handler.DeviceManage
{
    /// <summary>
    /// 设备类型录入处理类
    /// </summary>
    public class CreateDeviceTypeHandler: IRequestHandler<CreateDeviceTypeCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<DeviceType> _deviceTypeRepository;
        private readonly IMapper _mapper;
        private readonly ICodeRuleService codeRuleService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deviceTypeRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="codeRuleService"></param>
        public CreateDeviceTypeHandler(IBaseRespository<DeviceType> deviceTypeRepository, IMapper mapper
            ,ICodeRuleService codeRuleService)
        {
            _deviceTypeRepository = deviceTypeRepository;
            _mapper = mapper;
            this.codeRuleService = codeRuleService; //编码
        }
        /// <summary>
        /// 处理设备类型类型录入请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(CreateDeviceTypeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //将命令转换为领域模型
                var deviceType = _mapper.Map<DeviceType>(request);
                //判断设备类型是否存在
                var deviceTypeExist = await _deviceTypeRepository.GetAll().AnyAsync(x => x.DeviceTypeName == deviceType.DeviceTypeName);
                if (deviceTypeExist != false)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "设备类型已存在"
                    };
                }

                //编码
                if (string.IsNullOrEmpty(deviceType.DeviceTypeCode))
                {
                    var code = await codeRuleService.AutoGenerateCodeAsync("ProduceBasicData.Domain.DeviceManage.DeviceType");
                    if (!string.IsNullOrEmpty(code))
                    {
                        deviceType.DeviceTypeCode= code;
                    }
                    else
                    {
                        deviceType.DeviceTypeCode = $"SB{DateTime.Now:yyyyMMddHHmmss}";
                    }
                }


                //保存设备类型
                var result = await _deviceTypeRepository.AddAsync(deviceType);
                if (result > 0)
                {
                    return new ApiResult<int>
                    {
                        Data = result,
                        Code = ApiEnum.Success,
                        Msg = "设备类型录入成功"
                    };
                }
                else
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "设备类型录入失败"
                    };
                }
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.Failed,
                    Msg = ex.Message
                };
            }
        }
    }
}
